<html>
<head><meta charset="utf-8"><title>string literal as const · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/string.20literal.20as.20const.html">string literal as const</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="229927450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/string%20literal%20as%20const/near/229927450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/string.20literal.20as.20const.html#229927450">(Mar 11 2021 at 21:17)</a>:</h4>
<p>Are there plans to make something like this work at some point?<br>
<a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=d97d647ffd300cca6fe8a0eb787dc73d">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=d97d647ffd300cca6fe8a0eb787dc73d</a></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">msg</span>: <span class="kp">&amp;</span><span class="kt">str</span> <span class="o">=</span><span class="w"> </span><span class="s">"Hello world!"</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="n">msg</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Just curious <span aria-label="innocent" class="emoji emoji-1f607" role="img" title="innocent">:innocent:</span></p>



<a name="229936314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/string%20literal%20as%20const/near/229936314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/string.20literal.20as.20const.html#229936314">(Mar 11 2021 at 22:08)</a>:</h4>
<p>I think that depends on some details of the question.</p>
<p>For example, I suspect this might never work:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">msg</span>: <span class="kp">&amp;</span><span class="kt">str</span> <span class="o">=</span><span class="w"> </span><span class="s">"Hello {}!"</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span><span class="w"> </span><span class="s">"world"</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>because mixing macros -- which are parse time -- with const prop -- which is after typechecking and name lookup and stuff -- is fundamentally awkward.</p>
<p>If it means <code>println!(msg)</code> as shorthand for <code>println!("{}", msg)</code>, then maybe?</p>



<a name="229982728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/string%20literal%20as%20const/near/229982728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/string.20literal.20as.20const.html#229982728">(Mar 12 2021 at 06:21)</a>:</h4>
<p>I see less value in <code>println!(some_string_constant)</code>, because there are plenty of ways to handle that easily. It's potentially doable, but I don't think we should, not when it's easy to just add <code>"{}", </code></p>



<a name="229982745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/string%20literal%20as%20const/near/229982745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/string.20literal.20as.20const.html#229982745">(Mar 12 2021 at 06:21)</a>:</h4>
<p>It's <em>theoretically</em> possible to move <code>println!</code> to happen at a later phase of compilation while still being statically checked, but that doesn't necessarily make it a good idea.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>